﻿using CalcSharp.Core.Containers;

namespace CalcSharp.Core.Memory
{
    public class MemoryUtils
    {
        public long GetMatrixSize(AMatrix m)
        {
            long size = m.Columns * m.Rows;

            if (m is FloatMatrix) size *= 4;
            else if (m is DoubleMatrix) size *= 8;
            else if (m is Int24Matrix) size *= 3;

            return size;
        }

        public long GetMatrixSize(AMatrix m, MtrxStorageType t)
        {
            long size = m.Columns * m.Rows;

            switch (t)
            {
                case MtrxStorageType.tdouble:
                    size *= 8;
                    break;
                case MtrxStorageType.tfloat:
                    size *= 4;
                    break;
                case MtrxStorageType.tint24:
                    size *= 3;
                    break;
            }

            return size;
        }

        public long GetSetSize(ASet s)
        {
            long size = s.Count;

            if (s is FloatSet) size *= 4;
            else if (s is DoubleSet) size *= 8;

            return size;
        }

        public long GetSetSize(ASet s, SetStorageType t)
        {
            long size = s.Count;

            switch (t)
            {
                case SetStorageType.mFloat:
                    size *= 4;
                    break;
                case SetStorageType.mDouble:
                    size *= 8;
                    break;
            }

            return size;
        }

        public static AMatrix Conv(AMatrix input, MtrxStorageType t)
        {
            AMatrix ret;
            int i, j;
            switch (t)
            {
                case MtrxStorageType.tdouble:
                    if (input is DoubleMatrix) return input;
                    ret = new DoubleMatrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tfloat:
                    if (input is FloatMatrix) return input;
                    ret = new FloatMatrix(input.Rows, input.Columns);
                    break;
                case MtrxStorageType.tint24:
                    if (input is Int24Matrix) return input;
                    ret = new Int24Matrix(input.Rows, input.Columns);
                    break;
                default:
                    ret = new FloatMatrix(input.Rows, input.Columns);
                    break;
            }

            for (i = 0; i < input.Rows; i++)
            {
                for (j = 0; j < input.Columns; j++)
                {
                    ret[i, j] = input[i, j];
                }
            }
            return ret;
        }

        public static ASet Conv(ASet input, SetStorageType t)
        {
            ASet ret;
            int i;
            switch (t)
            {
                case SetStorageType.mDouble:
                    if (input is DoubleSet) return input;
                    ret = new DoubleSet(input.Count);
                    break;
                case SetStorageType.mFloat:
                    if (input is FloatSet) return input;
                    ret = new FloatSet(input.Count);
                    break;
                default:
                    ret = new FloatSet(input.Count);
                    break;
            }

            for (i = 0; i < input.Count; i++)
            {
                ret[i] = input[i];
            }
            return ret;
        }
    }
}